package com.example.aigc_education.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.aigc_education.anno.RequestKeyParam;
import com.example.aigc_education.anno.RequestLock;
import com.example.aigc_education.domain.Course;
import com.example.aigc_education.domain.dto.ChapterDTO;
import com.example.aigc_education.domain.dto.CourseDTO;
import com.example.aigc_education.domain.dto.CourseMongoDto;
import com.example.aigc_education.domain.vo.CourseVO;
import com.example.aigc_education.domain.vo.KnowledgePointerVO;
import com.example.aigc_education.service.CourseService;
import com.example.aigc_education.service.UserCourseService;
import com.example.aigc_education.utils.ResultUtil;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/course")
@RestController
public class CourseController {

    @Resource
    private CourseService courseService;

    @Resource
    private UserCourseService userCourseService;


    /**
     *  添加课表信息
     * @param courseMongoDto    课表dto对象
     * @return                  返回添加成功信息和课表id
     */
    @RequestLock(prefix = "addCourse:",expire = 3)
    @PostMapping("/addCourse")
    public ResultUtil addCourse(@RequestKeyParam @RequestBody CourseMongoDto courseMongoDto){
        //Course newCourse = courseMongoService.addCourse(course);
        courseService.addCourseInMongo(courseMongoDto);

        return ResultUtil.success("添加成功");
    }


    /**
     *  展示课表的全部信息
     * @return      集合——课表信息
     */
    @GetMapping("/listCourse")
    public ResultUtil listCourse(Integer year, Integer semester){
        CourseVO courseList = courseService.selectAllCourseInMongo(year, semester );
        return ResultUtil.success(courseList);
    }


    /**
     *  更新课程信息
     * @param course    待更新的课表信息
     * @return          返回修改成功的信息和课表id
     */
/*    @PutMapping("/updateCourse")
    public ResultUtil updateCourse(@RequestBody Course course){
        Course newCourse = courseService.updateCourseInMongo(course);

        // 判断是否存在课程信息，存在返回修改成功，不存在返回错误信息
        if ( newCourse != null ){

            String courseId = newCourse.getCourseId();
            return ResultUtil.success("修改成功", courseId);

        }

        return ResultUtil.error("查无此课程");
    }*/


    /**
     *  删除课表信息
     * @param courseId  课表id
     * @return          返回删除成功信息
     */
/*    @DeleteMapping("/deleteCourse")
    public ResultUtil deleteCourse(String courseId){
        courseService.deleteCourseByIdInMongo(courseId);

        return ResultUtil.success("删除成功");
    }*/


    /**
     *  根据课程id查找课程
     * @param courseId      课程id
     * @return              返回course
     */
  /*  @GetMapping("/queryId/{courseId}")
    public ResultUtil selectCourseById(@PathVariable String courseId){
        Course course = courseService.selectCourseByIdInMongo(courseId);

        return ResultUtil.success(course);
    }
*/

    /**
     *  根据据周查询
     * @param year      学年
     * @param semester  学期
     * @param week      查询德周
     * @return          course的VO随想
     */
    @PostMapping("/queryWeek")
    public ResultUtil selectCourseByWeek( Integer year, Integer semester, Integer week ){
        // 查找课程
        CourseVO course = courseService.selectCourseByWeekInMongo(year, semester, week);


/*        // 判断课程是否存在
        // 集合不为空——存在课程
        if ( !course.getCourseList().isEmpty() ){
            return ResultUtil.success(course);
        }*/

        // 集合为空——课程不存在
        return ResultUtil.success(course);
    }


    /**
     *  根据日期查找这一周的课表信息
     * @param date          日期
     * @return              course的VO对象
     */
    @GetMapping("/queryWeek/{date}")
    public ResultUtil selectCourseByDate(@PathVariable("date") String date){
        // 解析日期————将字符串转化为LocalDate
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate = LocalDate.parse(date, dateTimeFormatter);


        // 根据date查找日期
        CourseVO courseVO = courseService.selectCourseByDate( localDate );

        // 判断课程是否存在
        if ( courseVO != null )
            return ResultUtil.success(courseVO);


        // 不存在返回“查无此课程”
        return ResultUtil.success("查无课程");

    }

    /**
     *  根据课程名字模糊查询
     * @param courseName    课程名字
     * @return              课程集合
     */
/*    @PostMapping("/queryName")
    public ResultUtil selectCourseByName(String courseName){
        List<Course> courseList = courseService.selectCourseByNameInMongo(courseName);

        // 判断是否存在课程
        // 集合不为空——存在课程信息
        if ( !courseList.isEmpty() ){
            return ResultUtil.success(courseList);
        }

        // 集合为空——不存在课程信息
        return ResultUtil.error("查无课程");
    }*/

    /**
     * 获取用户在学课程(没数据)
     * @return 返回用户在学课程
     */
/*    @GetMapping("/byUser")
    public ResultUtil getCourseByUser() {
        Long userId = SecurityUtils.getUserId();
        List<String> coursesList = userCourseService.selectCourseNameByUserId(userId);
        return ResultUtil.success(coursesList);
    }*/

    /**
     * 展示课程中有哪些章节和知识点
     * @param courseName 课程名
     */
    @GetMapping("/knowledgePointer")
    public ResultUtil getKnowledgePointer(String courseName) {
        Course course = courseService.getOne(new LambdaQueryWrapper<Course>().eq(Course::getCourseName, courseName));
        KnowledgePointerVO knowledgePointer = new KnowledgePointerVO();
        knowledgePointer.setCourseName(course.getCourseName());
        List<CourseDTO> courseList = JSON.parseArray(course.getChapter(), CourseDTO.class);
        List<ChapterDTO> chapters = courseList.stream().map(courseDTO -> {
            ChapterDTO chapter = new ChapterDTO();
            chapter.setChapter(courseDTO.getChapter().getChapter());
            chapter.setKnowledgePointers(courseDTO.getChapter().getKnowledgePointers());
            return chapter;
        }).collect(Collectors.toList());
        knowledgePointer.setChapters(chapters);
        return ResultUtil.success(knowledgePointer);
    }
}
